﻿Imports Entidades
Public Class frmAñadirEntrega
    Private listaUsuarios As List(Of Usuario)
    Private Sub frmAñadirEntrega_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        listaUsuarios = miGestion.obtenerUsuarios
        lblFecha.Text = "Se almacenará con fecha: " & Today.Date
        For i As Integer = 0 To listaUsuarios.Count - 1
            cboUsuarios.Items.Add(listaUsuarios(i).Nombre)
        Next
    End Sub
    Private Sub txt2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumero.KeyPress
        If Not IsNumeric(e.KeyChar) AndAlso e.KeyChar <> ControlChars.Back Then
            MessageBox.Show("Solo admito dígitos")
            e.KeyChar = ""
        End If
    End Sub

    Private Sub btnAñadir_Click(sender As Object, e As EventArgs) Handles btnAñadir.Click
        If txtTelefono.Text = "" AndAlso txtEmail.Text = "" Then
            MessageBox.Show("Atencion, Debe indicar el email o el telefono")
            Exit Sub
        End If
        If txtTelefono.Text = "" Then

        ElseIf Not txtTelefono.Text.Length = 9 OrElse (txtTelefono.Text.StartsWith("9")) AndAlso txtTelefono.Text.StartsWith("7") AndAlso
            txtTelefono.Text.StartsWith("6") Then
            MessageBox.Show("Telefono incorrecto")
            Exit Sub

        End If
        If txtEmail.Text = "" Then

        ElseIf Not txtEmail.Text.Contains("@") OrElse Not txtEmail.Text.Contains(".") Then
            MessageBox.Show("Email incorrecto")
            Exit Sub
        End If

        If txtEmail.Text = "" AndAlso txtTelefono.Text = "" Then
            MessageBox.Show("Debe establecer un campo de contacto del cliente válido")
            Exit Sub
        End If

        If txtNumero.Text = "" Then
            MessageBox.Show("Atencion, faltan campos por rellenar")
            Exit Sub
        End If
        If cboUsuarios.Text = "" Then
            MessageBox.Show("Atencion, faltan campos por rellenar")
            Exit Sub
        End If
        If Integer.Parse(txtNumero.Text) <= 0 Then
            MessageBox.Show("La entrega debe tener minimo un objeto")
            Exit Sub
        End If

        Dim usu As Usuario
        For i As Integer = 0 To listaUsuarios.Count - 1
            If listaUsuarios(i).Nombre = cboUsuarios.Text Then
                usu = listaUsuarios(i)
            End If
        Next
        Dim cli As Cliente = miGestion.clientePorTelefonoEmail(txtEmail.Text, txtTelefono.Text)
        If IsNothing(cli) Then
            Dim result2 As Integer = MessageBox.Show("No existe ese cliente, ¿Desea añadirlo?", "Atencion", MessageBoxButtons.YesNo)
            If result2 = DialogResult.Yes Then
                frmAñCliente.ShowDialog()
                Me.Close()



            End If
            Exit Sub
        End If
        Dim entrega As New Entrega(cli.IdCliente, Integer.Parse(txtNumero.Text), usu.IdUsuario)
        Dim mens As String = miGestion.añadirEntrega(entrega)
        If mens <> "" Then

            MessageBox.Show(mens)
            Exit Sub
        End If
        Dim listaEntregas = miGestion.entregasPorCliente(cli)
        entrega = listaEntregas(listaEntregas.Count - 1)
        Dim result As Integer = MessageBox.Show("Entrega añadida correctamente. ¿Desea registrar sus objetos ahora?", "Atencion", MessageBoxButtons.YesNo)
        If result = DialogResult.Yes Then
            Dim frm As New frmAñadirObjeto
            frm.entrega = entrega
            frm.ShowDialog()


        End If
    End Sub

    Private Sub btnCancelar_Click(sender As Object, e As EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Close()

    End Sub
End Class